<?php
/**
 *   Abstract class for controllers
 *
 *   @author Renan Henrique Abreu Abreu <renanhabreu@gmail.com>
 *   @version 2.0
 *   @abstract
 *   @package Nucleo
 *   @subpackage Nucleo.Controller
 *   @copyright 2009-2010 Renan Abreu
 */
abstract class Nucleo_Controller_Controller{
	/**
	 * Default VIEW name for controller
	 * @var String
	 */
	private   $filename   = null;
	/**
	 * Helper for Views
	 */
	protected $helper = null;
	
	public function __construct(){
		$this->filename = 'index';
		$this->helper =  new Nucleo_Controller_Helper();
		$this->loadHelper('View');
	}
	/**
	 * Build Url for application
	 * 
	 * @param string $parameters
	 * @param boolean $echo
	 */
	static public function url($parameters,$echo = false){
		
		$url = NULL;
		$index = '';

		if($parameters){
			$params = '';
			foreach($parameters as $title=>$value){
				switch (strtolower($title)){
					case 'action':
						$params .= 'a/'.$value.'/';
						break;
					case 'module':
						$params .='m/'.$value.'/';
						break;
					case 'controller':
						$params .='c/'.$value.'/';
						break;
					default:
						$params .= $title.'/'.$value.'/';
						break;
				}
			}
			if(!MOD_REWRITE)
				$index = 'index.php?';
			$url = BASE_APPLICATION.$index.$params;
		}
		
		if($echo)		
			echo $url;
		else
			return $url;
	}
	
	/**
	 * Mount the template
	 * 
	 * @access protected
	 * @param String name file that will read
	 * @param Boolean if TRUE the template will print in screen. Default[true]
	 * @param Boolean if TRUE the  file not is locales default
	 * @return String HTML content
	 * @deprecated deprecated sice version 2.0.4
	 */
	protected function output($file = null,$echo = true,$external = false) {
		
		//Comunicando com a camada VIEW
		if(!$file)   
			$file = MODULE_PATH.Nucleo_Controller_Factory::$modulo.'/View/'.$this->filename;
		elseif($external == false){
			$fileView = MODULE_PATH.Nucleo_Controller_Factory::$modulo.'/View/'.$file;
			
			if(!file_exists($fileView.'.'.TEMPLATE_NAME))
				$file = TEMPLATE_PATH.$file;
			else
				$file = $fileView;
		}
		
		///////////////////////////////
		
		return $this->display($file,$echo);
	}
	
	/**
	 * display external template file
	 * @param string $file
	 * @param boolean $echo
	 * @return string HTML
	 */
	protected function outputExtView($file,$echo = true){
		return $this->display($file,$echo,'External view');
	}
	
	/**
	 * Display public template
	 * @param string $file
	 * @param boolean $echo
	 * @return string HTML
	 */
	protected function outputPubView($file,$echo = true){
		$file = TEMPLATE_PATH.$file;
		return $this->display($file,$echo,'Public view');
	}
	
	/**
	 * Display Module template
	 * @param string $file
	 * @param boolean $echo
	 * @return string HTML
	 */
	protected function outputModView($file = null, $echo = true){
		//Comunicando com a camada VIEW
		if(!$file)   
			$file = MODULE_PATH.Nucleo_Controller_Factory::$modulo.'/View/'.$this->filename; //Nome default
		else			
			$file = MODULE_PATH.Nucleo_Controller_Factory::$modulo.'/View/'.$file;
		
		return $this->display($file,$echo, 'Module view');
	}

	/**
	 * Display template
	 * @param string template dir 
	 * @param boolean
	 * @param boolean
	 */
	private function display($file,$echo,$type){
		///////////////////////////////
		$file .= '.'.TEMPLATE_NAME;
		
		ob_start();      //Inicializa o buffer              
		
		$contents = null;
		if(file_exists($file) or $file == null){
			include($file);                
			$contents = ob_get_contents(); //Carrega o buffer para a variavel
			
			//Adiciona scripts, bibliotecas e estilos 			
			if($this->helper->exist('View')){
				if($this->helper->View->getHeader() != null)
					$contents = str_ireplace('</head>',$this->helper->View->getHeader().'</head>',$contents);
					
		
			}
		}else{
			throw new Nucleo_Exception_Template($file,$type);
		}
		ob_end_clean();  //Limpa buffer
		
		
		if($echo)
			echo $contents;
		else
			return $contents;           
	}
		
	/**
	 * Responsable for add stylesheet
	 * 
	 * @param String Stylesheet name
	 * @param Boolean TRUE para em PUBLIC_PATH ou FALSE para VIEW ou pasta externa
	 * @param Boolean true se o arquivo nao estiver nas pastas padrões de estilos
	 * @deprecated 2.5 - 08/04/2010
	 */
	public function addStyle($nameStyle,$public = false,$external = false){
		
		if(!$this->helper->exist('View'))
			$this->loadHelper('View');
		$this->helper->View->addStyle($nameStyle,$public,$external);
	}
	
	/**
	 * Responsable for add scripts
	 *  
	 * @param String  Nome do Script
	 * @param Boolean TRUE para em PUBLIC_PATH ou FALSE para VIEW ou pasta externa
	 * @param Boolean true se o arquivo nao estiver nas pastas padrões de scripts
	 * @param String elemento TYPE da tag html script
	 * @deprecated 2.5 - 08/04/2010
	 */
	public function addScript($nameScript,$public = false,$external = false,$type='text/javascript'){
		
		if(!$this->helper->exist('View'))
			$this->loadHelper('View');
		$this->helper->View->addScript($nameScript,$public,$external,$type);
	}
		
	/**
	 * Will add images
	 * 
	 * @param String Nome da Imagem
	 * @param String ID for tag HTML
	 * @param Array Options for img
	 * @param Boolean TRUE for PUBLIC_PATH. FALSE for VIEW or path external
	 * @param Boolean TRUE if file no is in default path of styles
	 * @deprecated 2.5 - 08/04/2010
	 */
	public function addImg($nameImg,$id,$arrayOptions = NULL,
		$public = false,$external = false){		
		
		if(!$this->helper->exist('View'))
			$this->loadHelper('View');
		$this->helper->View->addImg($nameImg,$id,$arrayOptions,$public,$external);
	}
	
	/**
	 * Will return the path location VIEW
	 * 
	 * @param string nome da constante a ser localizada
	 * @param boolean caso se refira a pasta publica
	 * @return string
	 * @deprecated 2.5 - 08/04/2010
	 */
	public function getPath($path,$public = false){
		
		if(!$this->helper->exist('View'))
			$this->loadHelper('View');
		return $this->helper->View->getPath($path,$public);	
	}
	
	/**
	 * Call Helper for controller and view
	 * 
	 * @param string|array helper name
	 */
	public function loadHelper($helperName){
		
		if(is_string($helperName)){
			$helpers = array($helperName);
		}elseif(is_array($helperName)){
			$helpers = $helperName;
		}else{
			throw new Nucleo_Exception_Sifra('033');
		}
		
		foreach($helpers as $helperName){
			if(Nucleo_Load_Helper::findClass($helperName) == true){
				
				try{
					$helper = $helperName.'_Helper';
					$this->helper->$helperName = new $helper();
					
				}catch (Exeption $e){
					echo 'Erro ao construir Helper';
					exit();
				}			
			}else{
				throw new Nucleo_Exception_Load('022');
			}
		}
	}
	
	
}
?>
